<?php 
//ArrowController::importModel("org.arrowplatform.interfaces.IArrowSingleton");

class ArrowTableColumnsManager  implements IArrowSingleton, IArrowControlsManager{

	
	private $registredColumns = array(
		"standard" => "org.arrowplatform.controls.presentation.table.columns.ArrowTableStandardColumn",
		"editable" => "org.arrowplatform.controls.presentation.table.columns.ArrowTableEditableColumn",
		"extensible" => "org.arrowplatform.controls.presentation.table.columns.ArrowTableExtensibleColumn",
		"template" => "org.arrowplatform.controls.presentation.table.columns.ArrowTableTemplateColumn",
		"tree" => "org.arrowplatform.controls.presentation.table.columns.ArrowTableTreeColumn",
		"menu" => "org.arrowplatform.controls.presentation.table.columns.ArrowTableMenuColumn",
		"access" => "org.arrowplatform.controls.presentation.table.columns.ArrowTableAccessShareColumn"	 	 
	);
	
	private $registredFilters = array(
		"text" => "org.arrowplatform.controls.presentation.table.filters.ArrowTableTextFilter",
		"select" => "org.arrowplatform.controls.presentation.table.filters.ArrowTableSelectFilter",
		"range" => "org.arrowplatform.controls.presentation.table.filters.ArrowTableRangeFilter",
		"date" => "org.arrowplatform.controls.presentation.table.filters.ArrowTableDateFilter",
		"boolean" => "org.arrowplatform.controls.presentation.table.filters.ArrowTableBooleanFilter"
	);
	
	
	/**
	 * Singleton static var
	 *
	 * @var ArrowUIActionsManager
	 */
	private static $selfInstance = null;
	
	/**
	 * Singleton
	 * 
	 * @return ArrowUIActionsManager
	 */
	public static function getDefault( ) {
		if (self :: $selfInstance == null ) {
			self :: $selfInstance = new ArrowTableColumnsManager();
		}
		return self :: $selfInstance;
	}
	
	public function columnHandle( $params, $prefix, $name ){
		if(!isset($params["type"]))
			$params["type"] = "standard";
		if( !isset($this->registredColumns[$params["type"]] ) ) {
	        throw new ArrowCoreException( "Table column type definition error[ type=".$params["type"]." not found ]"  ) ;
	    }
	    
		$model = $this->registredColumns[$params["type"]];
		$class = explode(".", $model);
		$class = array_pop($class);
		ArrowController::importModel($model);
		
		$fieldObj = new $class( $params, $prefix, $name, ArrowView::getCurrentView()->getTemplateDescriptor()->getKey(),$class );
		return array( "model" => $model, "object"=>$fieldObj );
	}
	
	public function filterHandle( $params, $prefix, $name ){
		if(!isset($params["type"]))
			$params["type"] = "text";
		if( !isset($this->registredFilters[$params["type"]] ) ) {
	        throw new ArrowCoreException( "Table column type definition error[ type=".$params["type"]." not found ]"  ) ;
	    }
	    
		$model = $this->registredFilters[$params["type"]];
		$class = explode(".", $model);
		$class = array_pop($class);
		ArrowController::importModel($model);
		
		$fieldObj = new $class( $params, $prefix, $name, ArrowView::getCurrentView()->getTemplateDescriptor()->getKey(), $class );
		return array( "model" => $model, "object"=>$fieldObj );
	}	
	
	
	public function getControlVersionsConf($prefix,$tag){
		if($tag == "column")
			$array = $this->registredColumns;
		else
			$array = $this->registredFilters;
		$tmp = array();
		foreach($array as $param => $action){
			if($tag == "column")
				$obj = $this->columnHandle( array("type" => $param), $prefix, $tag  );
			else
				$obj = $this->filterHandle( array("type" => $param), $prefix, $tag  );
			$obj["object"]->configure();
			$params = array_merge( $obj["object"]->getProperties() ,array( "type" => $param ));
			$tmp[$tag.":".$param] = array(
				"params" => $params,
				"requiredParams" => $obj["object"]->getRequiredProperties(),
				"default" => 'type="'.$param.'"'
			); 
		}
		return $tmp;
	}
	
} 
?>